package com.guanyong.webapi.controllers.api;

import com.guanyong.utils.Comm.ResultCode;
import com.guanyong.utils.Comm.ResultObject;
import com.guanyong.webapi.config.SMSUtil;
import com.guanyong.webapi.config.SmsModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/7/3 0003.
 */
@RestController
@RequestMapping("/api/sms/")
public class SmsApi {

    @Autowired
    SMSUtil smsUtil;

    @RequestMapping(value = "getRanNum", method = RequestMethod.POST)
    public ResultObject<String> AddPlayResut(HttpServletRequest request,
                                              @RequestParam String phoneNum,
                                              @RequestParam String phoneSN) {
        ResultObject<String> resultObject = new ResultObject<String>();
        HashMap<String,SmsModel> smsMap =   smsUtil.getSmsModelMap();
        if (smsMap.get(phoneSN)!=null){
            SmsModel smsModel =smsMap.get(phoneSN);
            if (smsModel.getInitTime().getTime() + 60*1000<new Date().getTime()){
                String number = smsUtil.sendSms(phoneNum,phoneSN);
                if (number!=null &&!number.equals("")){
                    resultObject.setResult(number);
                    resultObject.setCode(ResultCode.OperateOk);
                }else{
                    resultObject.setCode(ResultCode.OperateFail);
                    resultObject.setErrorMsg("短信发送失败");
                }
            }else {
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("短信获取过于频繁");
            }
        }else{
            String number = smsUtil.sendSms(phoneNum,phoneSN);
            if (number!=null &&!number.equals("")){
                resultObject.setResult(number);
                resultObject.setCode(ResultCode.OperateOk);
            }else{
                resultObject.setCode(ResultCode.OperateFail);
                resultObject.setErrorMsg("短信发送失败");
            }
        }



        return resultObject;
    }


    @RequestMapping(value = "verificationNum", method = RequestMethod.POST)
    public ResultObject<String> verificationNum(HttpServletRequest request,
                                             @RequestParam String phoneNum,
                                             @RequestParam String phoneSN,
                                                @RequestParam String number) {
        ResultObject<String> resultObject = new ResultObject<String>();

        HashMap<String,SmsModel> smsMap =   smsUtil.getSmsModelMap();
        SmsModel smsModel = smsMap.get(phoneSN);
        if (smsModel!=null){
            if (smsModel.getNumber().equals(number)){
                if ((smsModel.getInitTime().getTime() + 15*60*1000)>new Date().getTime()){
                    resultObject.setResult(number);
                    resultObject.setCode(ResultCode.OperateOk);
                }else {
                    resultObject.setErrorMsg("验证码过期");
                    resultObject.setCode(ResultCode.OperateFail);
                }
                smsMap.remove(phoneSN);
            }else {
                resultObject.setErrorMsg("验证码错误");
                resultObject.setCode(ResultCode.OperateFail);
            }
        }else {
            resultObject.setErrorMsg("验证码无效");
            resultObject.setCode(ResultCode.OperateFail);
        }

        return resultObject;
    }
}
